﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wq.Extensions.Common;
using Wq.Model;

namespace Wq.Extensions
{
    /// <summary>
    /// 注入事件
    /// </summary>
    public static class FrameworkSetup
    {
        public static void AddFrameworkSetup(this IServiceCollection services)
        {
            var serviceScopeds = RuntimeHelper.GetProjectTypes()
                .Where(p => !p.IsInterface && !p.IsAbstract && !p.IsSealed)
                .Where(p => typeof(IAddScoped).IsAssignableFrom(p))
                .ToList();

            foreach (var serviceType in serviceScopeds)
                services.AddScoped(serviceType);

            var serviceSingletons = RuntimeHelper.GetProjectTypes()
               .Where(p => !p.IsInterface && !p.IsAbstract && !p.IsSealed)
               .Where(p => typeof(IAddSingleton).IsAssignableFrom(p))
               .ToList();

            foreach (var serviceType in serviceSingletons)
                services.AddSingleton(serviceType);

            var serviceTransients = RuntimeHelper.GetProjectTypes()
              .Where(p => !p.IsInterface && !p.IsAbstract && !p.IsSealed)
              .Where(p => typeof(IAddTransient).IsAssignableFrom(p))
              .ToList();

            foreach (var serviceType in serviceTransients)
                services.AddTransient(serviceType);
        }
    }
}
